<?php 

namespace app\common\shop;

use think\Db;
use think\Model;
use traits\model\SoftDelete;


class OrderGoodsModel extends model {
    //use SoftDelete;//开启了软删除
    //protected  = 'del_time';
    // 完整数据表名
    protected $table = 'shop_order_goods';


    /**
     * [建表]
     * @return
     */
    public static function createTable(){
        return Db::query("CREATE TABLE If Not Exists `shop_order_goods` (
              `id` int(11) NOT NULL AUTO_INCREMENT,
              `uid` int(11) NOT NULL COMMENT '会员id',
              `ordersn` varchar(255) NOT NULL COMMENT '订单编号',
              `orderid` int(11) NOT NULL DEFAULT '0' COMMENT '订单id',
              `goodsid` int(11) NOT NULL DEFAULT '0' COMMENT '商品id',
              `specid` int(11) NOT NULL DEFAULT '0' COMMENT '规格id',
              `spectitle` varchar(255) DEFAULT NULL COMMENT '规格名称',
              `number` int(11) NOT NULL DEFAULT '0' COMMENT '下单数量',
              `price` decimal(22,2) NOT NULL DEFAULT '0.00' COMMENT '价格',
              `alltotal` decimal(22,2) NOT NULL DEFAULT '0.00' COMMENT '总价',
              `createtime` int(10) unsigned NOT NULL DEFAULT '0' COMMENT '时间',
              `shoptype` int(10) unsigned NOT NULL DEFAULT '1' COMMENT '商城类型',
              PRIMARY KEY (`id`)
            ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COMMENT='订单商品表'");
    }

 	/**
     * [添加]
     * @return
    */
    public static function add($data){
    	$data['createtime'] = time();
    	return self::insertGetId($data);
    }

    /**
     * [修改]
     * @return
    */
    public static function edit($data,$id){
        $data['createtime'] = time();
        return self::where('id',$id)->update($data);
    }

    /**
     * [获取订单商品信息]
     * @return
    */
    public static function getOrderGoods($value){
        $goods =
                OrderGoodsModel::alias('og')
                ->field('og.id,og.price,og.goodsid,og.specid,og.number,og.price,og.alltotal,og.spectitle,g.title,g.path')
                ->join('shop_goods g','og.goodsid = g.id','left')
                ->where('og.orderid',$value['id'])
                ->select() 
                ->toArray();
        foreach ($goods as $k => $v) {
            $path = unserialize($v['path']);
            $goods[$k]['path'] = $path;
            $goods[$k]['img'] = $path[0];
        }
        return $goods;
    }
}